﻿//------------------------------------------------------------------------------------------
// File: <FTPropPackage.cs>
// Purpose: implement <Some property definitions for the reactor unit.>
//
// @author <Yun Hua>
// @version 1.0 2010/04/26
// 
// Copyright (C) 2010, YUN HUA.
//-----------------------------------------------------------------------------------------//

using System;
using System.Collections.Generic;
using System.Text;

namespace Yashen.SimLab
{
    internal class FTPropPackage
    {
        #region Public Constants

        public static readonly int INDEX_H2 = 0;
        public static readonly int INDEX_CO = 1;
        public static readonly int INDEX_H2O = 2;
        public static readonly int FT_COMPONENT_COUNT = 48;
        public static readonly double MOLAR_GAS_CONSTANT = 8.314472;  // J / mol / K
        public static readonly double PRESSURE_ATM = 1.01325 * 10000; // Pa

        #endregion

        #region Private Members

        private static List<KeyValuePair<double/*temperature*/, double/*STY*/>> spaceTimeYields;

        #endregion

        #region Public Properties

        public static List<KeyValuePair<double, double>> SpaceTimeYields
        {
            get { return spaceTimeYields; }
            set { spaceTimeYields = value; }
        }

        #endregion

        /// <summary>
        /// Stoichiometry Coefficients actually used: H2
        /// </summary>
        public static readonly double[] StoiCoefH2 = {-3,
            -5,
            -7,
            -9,
            -11,
            -13,
            -15,
            -17,
            -19,
            -21,
            -23,
            -25,
            -27,
            -29,
            -31,
            -33,
            -35,
            -37,
            -39,
            -41,
            -43,
            -45,
            -47,
            -49,
            -51,
            -53,
            -55,
            -57,
            -59,
            -61,
            -64,
            -68,
            -72,
            -76,
            -80,
            -84,
            -88,
            -93,
            -99,
            -105,
            -111,
            -117,
            -123,
            -148,
            1
            };

        /// <summary>
        /// Stoichiometry Coefficients actually used: CO
        /// </summary>
        public static readonly double[] StoiCoefCO = {-1,
            -2,
            -3,
            -4,
            -5,
            -6,
            -7,
            -8,
            -9,
            -10,
            -11,
            -12,
            -13,
            -14,
            -15,
            -16,
            -17,
            -18,
            -19,
            -20,
            -21,
            -22,
            -23,
            -24,
            -25,
            -26,
            -27,
            -28,
            -29,
            -30,
            -31.5,
            -33.5,
            -35.5,
            -37.5,
            -39.5,
            -41.5,
            -43.5,
            -46,
            -49,
            -52,
            -55,
            -58,
            -61,
            -73,
            1
            };

        /// <summary>
        /// Stoichiometry Coefficients actually used: H2O
        /// </summary>
        public static readonly double[] StoiCoefH2O = {1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            15,
            16,
            17,
            18,
            19,
            20,
            21,
            22,
            23,
            24,
            25,
            26,
            27,
            28,
            29,
            30,
            31.5,
            33.5,
            35.5,
            37.5,
            39.5,
            41.5,
            43.5,
            46,
            49,
            52,
            55,
            58,
            61,
            73,
            -1
            };

        /// <summary>
        /// Stoichiometry Coefficients actually used: Cn
        /// </summary>
        public static readonly double[] StoiCoefCn = {1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1
            };

        /// <summary>
        /// Heat of formation, input Standard heat of formation (298 K)
        /// </summary>
        public static readonly double[] StdHeatFormIn = {0.0,  // H2
            -110530.0,  // CO
            -241810.0,  // H2O
            -74520.0,   // CH4
            -83820.0, 
            -104680.0, 
            -125790.0, 
            -146760.0, 
            -166940.0, 
            -187650.0, 
            -208750.0, 
            -228740.0, 
            -249460.0, 
            -270430.0, 
            -290720.0, 
            -311770.0, 
            -332440.0, 
            -353110.0, 
            -374170.0, 
            -394450.0, 
            -415120.0, 
            -435790.0, 
            -456460.0, 
            -460210.0, 
            -437110.0, 
            -448390.0, 
            -469580.0, 
            -489090.0, 
            -507740.0, 
            -525030.0, 
            -552700.0, 
            -567600.0, 
            -593850.0, 
            -626870.0, 
            -662940.0, 
            -674980.0, 
            -703900.0, 
            -753310.0, 
            -803450.0, 
            -850020.0, 
            -913160.0, 
            -984720.0, 
            -1039300.0, 
            -1026300.0, 
            -1000600.0, 
            -965300.0, 
            -905770.0, 
            -393510.0 
        };

        public static readonly string[] ConvertedIDs ={"CH4",
            "C2H6",
            "C3H8",
            "C4H10",
            "C5H12",
            "C6H14",
            "C7H16",
            "C8H18",
            "C9H20",
            "C10H22",
            "C11H24",
            "C12H26",
            "C13H28",
            "C14H30",
            "C15H32",
            "C16H34",
            "C17H36",
            "C18H38",
            "C19H40",
            "C20H42",
            "C21",
            "C22",
            "C23",
            "C24",
            "C25",
            "C26",
            "C27",
            "C28",
            "C29",
            "C30",
            "C31-C32",
            "C33-C34",
            "C35-C36",
            "C37-C38",
            "C39-C40",
            "C41-C42",
            "C43-C44",
            "C45-C47",
            "C48-C50",
            "C51-C53",
            "C54-C56",
            "C57-C59",
            "C60-C62",
            "C62+",
            "CO2"
         };

        #region Public Static Methods

        /// <summary>
        /// Calculate the Xco coupling with STY and GHSV.
        /// </summary>
        /// <param name="kCoeff">k coefficient.</param>
        /// <param name="STY">Space time yield in g/(L(cata)*h). </param>
        /// <param name="reactorVol">The reactor volume in L.</param>
        /// <param name="totalFlow">The total flow rate in mol/s.</param>
        /// <param name="temperature">The temperature in K.</param>
        /// <param name="pressure">The pressure in Pa.</param>
        /// <returns>CO conversion</returns>
        public static double CalcXcoUsingSTY(double kCoeff, double STY, double reactorVol, 
            double totalFlow, double temperature, double pressure)
        {
            return kCoeff * (STY / 0.6/*kg/L*/ / 1000) * (reactorVol / 1000) / 
                (totalFlow * FTPropPackage.MOLAR_GAS_CONSTANT * temperature / pressure) / 3600;
        }

        /// <summary>
        /// Calculate the Xch4 (CH4 yield) coupling with STY and GHSV and H2/CO ratio.
        /// X1(T, β) = k1* STY(Tw)*F1(β)/GHSV, for CH4
        /// F1(β) =( 0.0972β – 0.0987)/ 0.060708 and F1(β) > 0
        /// β = H2 / CO.
        /// </summary>
        /// <param name="kCoeff">k1 coefficient.</param>
        /// <param name="STY">Space time yield in g/(L(cata)*h). </param>
        /// <param name="reactorVol">The reactor volume in L.</param>
        /// <param name="totalFlow">The total flow rate in mol/s.</param>
        /// <param name="temperature">The temperature in K.</param>
        /// <param name="pressure">The pressure in Pa.</param>
        /// <param name="beta">H2 / CO ratio.</param>
        /// <returns>CO conversion</returns>
        public static double CalcYCH4UsingH2CORatio(double k1, double STY, double reactorVol,
            double totalFlow, double temperature, double pressure, double beta)
        {
            double f1 = (0.0972 * beta - 0.0987) / 0.060708; 
            double res = k1 * (STY / 0.6/*kg/L*/ / 1000) * f1 * (reactorVol / 1000) /
                (totalFlow * FTPropPackage.MOLAR_GAS_CONSTANT * temperature / pressure) / 3600;
            if (res < 0)
            {
                res = 0;
            }
            return res;
        }

        /// <summary>
        /// Calculate the Xc2+ (total C2+ yield) coupling with STY and GHSV and H2/CO ratio.
        /// X2+(T, β) = k2* STY(Tw)*F2(β)/GHSV, for all the C2+ species
        /// F2(β) = (0.142β +0.1033)/ 0.33618
        /// β = H2 / CO.
        /// </summary>
        /// <param name="kCoeff">k2 coefficient.</param>
        /// <param name="STY">Space time yield in g/(L(cata)*h). </param>
        /// <param name="reactorVol">The reactor volume in L.</param>
        /// <param name="totalFlow">The total flow rate in mol/s.</param>
        /// <param name="temperature">The temperature in K.</param>
        /// <param name="pressure">The pressure in Pa.</param>
        /// <param name="beta">H2 / CO ratio.</param>
        /// <returns>CO conversion</returns>
        public static double CalcYC2PlusUsingH2CORatio(double k2, double STY, double reactorVol,
            double totalFlow, double temperature, double pressure, double beta)
        {
            double f2 = (0.142 * beta + 0.1033) / 0.33618;
            return k2 * (STY / 0.6/*kg/L*/ / 1000) * f2 * (reactorVol / 1000) /
                (totalFlow * FTPropPackage.MOLAR_GAS_CONSTANT * temperature / pressure) / 3600;
        }

        /// <summary>
        /// Get the space time yield by checking temperature-STY table using interpolation. 
        /// </summary>
        /// <param name="lsSTY">The target temperature in C.</param>
        /// <param name="stys">The space time yield table.</param>
        /// <returns>STY value.</returns>
        public static double GetSTYByTemperature(double temperature, 
            List<KeyValuePair<double/*temperature*/, double/*STY*/>> stys)
        {
            if (stys == null || stys.Count == 0)
            {
                return 100;
            }

            if (stys.Count == 1)
            {
                return stys[0].Value;
            }


            if (temperature <= stys[0].Key)
            {
                return stys[0].Value;
            }

            for (int i = 0; i < stys.Count - 1; ++i)
            {
                if (temperature <= stys[i + 1].Key)
                {
                    return stys[i].Value + (stys[i + 1].Value - stys[i].Value) *
                        (temperature - stys[i].Key) / (stys[i + 1].Key - stys[i].Key); 
                }
            }

            return stys[stys.Count - 1].Value;
        }

        #endregion
    }
}
